package io.renren.modules.diycake.service.impl;

import io.renren.modules.diycake.entity.DiyUserEntity;
import io.renren.modules.diycake.entity.ShopperEntity;
import io.renren.modules.diycake.service.ShopperService;
import io.renren.modules.diycake.service.UserService;
import io.renren.modules.diycake.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.diycake.dao.OrderDao;
import io.renren.modules.diycake.entity.OrderEntity;
import io.renren.modules.diycake.service.OrderService;
import org.springframework.util.ObjectUtils;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
@Autowired
private UserService userService;
@Autowired
private ShopperService shopperService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        String key = params.get("key") + "";

        if (!ObjectUtils.isEmpty(key)) {
            wrapper.like("order_name",key);
        }

        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                wrapper
        );
        List<OrderVo> collect = page.getRecords().stream().map(item -> {
            OrderVo orderVo = getOrderVo(item);
            return orderVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);

        return pageUtils;
    }

    public OrderVo getOrderVo(OrderEntity item) {
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(item,orderVo);
        List<DiyUserEntity> userEntityList = userService.list(null);
        List<ShopperEntity> shopperEntities = shopperService.list(null);
        System.out.println(userEntityList);
        userEntityList.forEach(item2 -> {
            if ( Long.valueOf(item2.getId()) == item.getUserId()) {
                orderVo.setUserName(item2.getName());

            }
        });
        shopperEntities.forEach(item2-> {
            if (item2.getId() == item.getShopId()) {
                orderVo.setShopName(item2.getName());
            }
        });
        return orderVo;
    }

}